local gui = require('gui')
local guidm = require('gui.dwarfmode')
local widgets = require('gui.widgets')


local function designateDig(pos)
    if (pos.x > 1 or pos.x < df.global.world.map.x_count) and (pos.y > 1 or pos.y < df.global.world.map.y_count) then
        local designation = dfhack.maps.getTileFlags(pos)
        designation.dig = df.tile_dig_designation.Default
    end
end

local function writeFile (str) 
    file = io.open("./text", "w+")
    file:write(str)
    file:close()
end

local function yu_gu_wa_func (pos) 
    for j=pos.y,1,-5 do
        for i=df.global.world.map.x_count-1,1,-1 do
            designateDig({x=i, y=j, z=df.global.window_z})
        end
    end
    for j=pos.y+5,df.global.world.map.y_count-1,5 do
        for i=df.global.world.map.x_count-1,1,-1 do
            designateDig({x=i, y=j, z=df.global.window_z})
        end
    end
    for i=df.global.world.map.y_count-1,1,-1 do
        designateDig({x=pos.x, y=i, z=df.global.window_z})
    end
end

local function bed_room_func (pos) 
    tmpn = {-10, 0 , 10}  
    for index,v in ipairs(tmpn) do 
        for i=pos.x-21,pos.x+21,1 do
            designateDig({x=i, y=pos.y + v, z=df.global.window_z})
        end
    end
    
    tmpx = {-22, 0 , 22}
    for index,v in ipairs(tmpx) do 
        for i=pos.y-10,pos.y+10,1 do
            designateDig({x=pos.x+v, y=i, z=df.global.window_z})
        end
    end

    tmpn2 = {3, -3, 7, -7}  
    tmpn3 = {1, -1, 9, -9}
    for index,v in ipairs(tmpn2) do 
        for i=pos.x-3,pos.x-21,-4 do
            designateDig({x=i, y=pos.y+tmpn3[index], z=df.global.window_z})
            designateDig({x=i, y=pos.y+v, z=df.global.window_z})
            designateDig({x=i-1, y=pos.y+v, z=df.global.window_z})
            designateDig({x=i+1, y=pos.y+v, z=df.global.window_z})
            designateDig({x=i, y=pos.y+v-1, z=df.global.window_z})
            designateDig({x=i-1, y=pos.y+v-1, z=df.global.window_z})
            designateDig({x=i+1, y=pos.y+v-1, z=df.global.window_z})
            designateDig({x=i, y=pos.y+v+1, z=df.global.window_z})
            designateDig({x=i-1, y=pos.y+v+1, z=df.global.window_z})
            designateDig({x=i+1, y=pos.y+v+1, z=df.global.window_z})
        end
        for i=pos.x+3,pos.x+21,4 do
            designateDig({x=i, y=pos.y+tmpn3[index], z=df.global.window_z})
            designateDig({x=i, y=pos.y+v, z=df.global.window_z})
            designateDig({x=i-1, y=pos.y+v, z=df.global.window_z})
            designateDig({x=i+1, y=pos.y+v, z=df.global.window_z})
            designateDig({x=i, y=pos.y+v-1, z=df.global.window_z})
            designateDig({x=i-1, y=pos.y+v-1, z=df.global.window_z})
            designateDig({x=i+1, y=pos.y+v-1, z=df.global.window_z})
            designateDig({x=i, y=pos.y+v+1, z=df.global.window_z})
            designateDig({x=i-1, y=pos.y+v+1, z=df.global.window_z})
            designateDig({x=i+1, y=pos.y+v+1, z=df.global.window_z})
        end
    end
    


end


print('area:')
print('    The z-level is z='..df.global.window_z)
print('    The cursor is at x='..df.global.cursor.x..', y='..df.global.cursor.y)
print('    The window is '..df.global.gps.dimx..' tiles wide and '..df.global.gps.dimy..' tiles high')
if df.global.gps.mouse_x == -1 then print('    The mouse is not in the DF window') else
print('    The mouse is at x='..df.global.gps.mouse_x..', y='..df.global.gps.mouse_y..' within the window') end

print('    globalX'..df.global.world.map.x_count..'  globalY'..df.global.world.map.y_count)






Shenxian = defclass(Shenxian, widgets.Window)
Shenxian.ATTRS {
    frame_title='Shenxian',
    frame={w=48, h=18, r=2, t=18},
    resizable=true,
    resize_min={h=10},
    autoarrange_subviews=true,
}

function Shenxian:init()
    self.mark = nil
    self:reset_double_click() 

    self:addviews{
        widgets.HotkeyLabel{
            frame={l=0},
            label='yu gu wa',
            key='CUSTOM_CTRL_D',
            auto_width=true,
            on_activate=self:callback('yu_gu_wa'),
            enabled=function() return dfhack.gui.getMousePos() end,
        },
        widgets.HotkeyLabel{
            frame={l=0},
            label='bed room',
            key='CUSTOM_CTRL_B',
            auto_width=true,
            on_activate=self:callback('bed_room_wa'),
            enabled=function() return dfhack.gui.getMousePos() end,
        }
    }
end

function Shenxian:reset_double_click()
    self.last_map_click_ms = 0
    self.last_map_click_pos = {}
end



local to_pen = dfhack.pen.parse
local CURSOR_PEN = to_pen{ch='o', fg=COLOR_BLUE,
                         tile=dfhack.screen.findGraphicsTile('CURSORS', 5, 22)}
local BOX_PEN = to_pen{ch='X', fg=COLOR_GREEN,
                       tile=dfhack.screen.findGraphicsTile('CURSORS', 0, 0)}
local SELECTED_PEN = to_pen{ch='I', fg=COLOR_GREEN,
                       tile=dfhack.screen.findGraphicsTile('CURSORS', 1, 2)}

function Shenxian:yu_gu_wa()
    self.active = "yes"
    self.workType = "yu gu wa"
end
function Shenxian:bed_room_wa()
    self.active = "yes"
    self.workType = "bed room"
end

function Shenxian:onInput(keys)
    if Shenxian.super.onInput(self, keys) then return true end
    if self.active ~= "yes" then return true end
    if keys._MOUSE_R_DOWN and self.mark then
        self.mark = nil
        self:updateLayout()
        return true
    elseif keys._MOUSE_L_DOWN then
        if self:getMouseFramePos() then return true end
        local pos = dfhack.gui.getMousePos()
        if not pos then
            self:reset_double_click()
            return false
        end

        local now_ms = dfhack.getTickCount()
        if same_xyz(pos, self.last_map_click_pos) and
                now_ms - self.last_map_click_ms <= 200 then
            self:reset_double_click()
            self.mark = nil
            self:updateLayout()
            return true
        end
        self.last_map_click_ms = now_ms
        self.last_map_click_pos = pos
        if self.mark then
            self:select_box(self:get_bounds(pos))
            self:reset_double_click()
            self.mark = nil
            self:updateLayout()
            return true
        end
        self.mark = pos
        if self.workType == "yu gu wa" then 
            yu_gu_wa_func(self.mark)
        elseif self.workType == "bed room" then
            bed_room_func(self.mark)
        end
        self:updateLayout()
        return true
    end
end

-- ShenXianScreen
ShenXianScreen = defclass(ShenXianScreen, gui.ZScreen)
ShenXianScreen.ATTRS {
    focus_path='index',
    pass_movement_keys=true,
    pass_mouse_clicks=false,
}

function ShenXianScreen:init()
    self:addviews{Shenxian{}}
end

function ShenXianScreen:onDismiss()
    view = nil
end

view = view and view:raise() or ShenXianScreen{}:show()
